<template>
  <div class="employee-container">
    <!-- 搜索区域 -->
    <div class="search-container">
      <div class="search-label">员工姓名：</div>
      <el-input v-model="params.name" clearable placeholder="请输入内容" class="search-main" @clear="getEmployeeList" />
      <el-button type="primary" @click="getEmployeeList">查询</el-button>
    </div>
    <div class="create-container">
      <el-button type="primary" @click="open">添加员工</el-button>
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table style="width: 100%" :data="employeeList">
        <el-table-column type="index" label="序号" width="70"/>
        <el-table-column label="员工姓名" width="140" prop="name" />
        <el-table-column label="登录账号" width="140" prop="userName" />
        <el-table-column label="联系方式" width="140" prop="phonenumber" />
        <el-table-column label="角色" width="140" prop="roleName" />
        <el-table-column label="状态" :formatter="fn" prop="status" width="100">
          <!-- <template #default="scope">
        {{ scope.row.status }}
      </template> -->
        </el-table-column>
        <el-table-column label="添加时间" prop="createTime" width="150"/>
        <el-table-column label="操作" fixed="right" width="140">
          <template #default="scope">
            <el-button size="mini" type="text" @click="edit(scope.row.id)">编辑</el-button>
            <el-button size="mini" type="text" @click="del(scope.row.id)">删除</el-button>
            <el-button size="mini" type="text" @click="resetPwd(scope.row.id)">重置密码</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="box">
      <!-- 分页器 -->
      <el-pagination :current-page="params.page" :page-sizes="[2, 4, 6, 8, 10]" :page-size="params.pageSize"
        layout="total,prev,pager,next,sizes" :total="total" @size-change="handleSizeChange"
        @current-change="handlecurrentChange" />
    </div>
    <!-- 添加员工弹窗-->
    <el-dialog :title="addForm.id?'编辑员工':'添加员工'" width="480px" :visible="dialogVisible" @close="closeDialog">
      <!-- 表单接口 -->
      <div class="form-container">
        <el-form ref="addForm" :model="addForm" :rules="addFormRules" label-width="80px">
          <el-form-item label="员工姓名" prop="name">
            <el-input v-model="addForm.name" />
          </el-form-item>
          <el-form-item label="登录账号" prop="userName">
            <el-input v-model="addForm.userName" />
          </el-form-item>
          <el-form-item label="联系方式" prop="phonenumber">
            <el-input v-model="addForm.phonenumber" />
          </el-form-item>
          <el-form-item label="分配角色" prop="roleId">
            <el-select v-model="addForm.roleId" placeholder="请选择角色">
              <el-option v-for="item in roleList" :key="item.roleId" :label="item.roleName" :value="item.roleId" />
            </el-select>
          </el-form-item>
          <el-form-item label="员工状态" prop="status">
            <el-radio-group v-model="addForm.status">
              <el-radio :label="0">禁用</el-radio>
              <el-radio :label="1">启用</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <el-button size="mini" @click="def">取 消</el-button>
        <el-button size="mini" type="primary" @click="submit">确 定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getEmployeeListAPI,getRoleListAPI,addEmployeeListAPI,delEmployeeListAPI,getEmployeeListDetailAPI,editEmployeeListAPI,resetPwdAPI} from '@/api/employee'
export default {
  name: 'Employee',
  data() {
    return {
      roleList:[],
      employeeList: [],
      params: {
        page: 1,
        pageSize: 10,
        name: '' // 员工姓名
      },
      total: 0,
      addForm: {
        name: '',
        userName: '',
        phonenumber: '',
        roleName: '',
        status: '',
        roleId:''
      },
      dialogVisible: false,
      addFormRules: {
      name: [
        { required: true, message: '请输入员工姓名', trigger: 'blur' }
      ],
      userName: [
        { required: true, message: '请输入登录账号', trigger: 'blur' }
      ],
      phonenumber: [
        { required: true, message: '请输入联系方式', trigger: 'blur' }
      ],
      roleId: [
        { required: true, message: '请分配角色', trigger: 'blur' }
      ],
      status: [
        { required: true, message: '请选择员工状态', trigger: 'blur' }
      ]
    }
    }
  },
  methods: {
    def(){
      this.dialogVisible = false
      this.clear()
    },
    resetPwd(id){
      this.$confirm('确定将密码重置为123456吗？',{type: 'warning'}).then(()=>{
        resetPwdAPI(id).then(()=>{
        this.$message.success('重置成功')
      })
      })
    },
    edit(id){
      this.dialogVisible=true
      getEmployeeListDetailAPI(id).then((res)=>{
        const {id,name,userName,phonenumber,roleName,status,roleId}=res.data
        this.addForm={id,name,userName,phonenumber,roleName,status,roleId}
      })
    },
    del(id){
      this.$confirm('删除员工后，该员工账号将无法登录，是否删除？',{type:'warning'}).then(()=>{
        delEmployeeListAPI(id).then(()=>{
          this.$message.success('删除成功')
          this.getEmployeeList()
        })
      })
    },
    clear(){
      this.addForm={
        name: '',
        userName: '',
        phonenumber: '',
        roleName: '',
        status: '',
        roleId:''
      }
    },
     submit(){
      this.$refs.addForm.validate((ok)=>{
         if(ok){
          if(this.addForm.id){
            editEmployeeListAPI(this.addForm).then(()=>{
              this.$message.success('编辑成功')
            this.clear()
            this.dialogVisible=false
            this.getEmployeeList()
            })
          }else{
            addEmployeeListAPI(this.addForm).then(()=>{
            this.$message.success('添加成功')
            this.clear()
            this.dialogVisible=false
            this.getEmployeeList()
          })
          }
        }
      })
    },
    getRoleList(){
      getRoleListAPI().then((res)=>{
        console.log(res);
        this.roleList=res.data
      })
    },
    closeDialog(){
      this.dialogVisible=false
      this.clear()
    },
    open(){
      this.dialogVisible=true
    },
    fn(row) {
      return row.status === 0 ? '禁用' : '启用'
    },
    handlecurrentChange(page) {
      this.params.page = page
      this.getEmployeeList()
    },
    handleSizeChange(pageSize) {
      this.params.pageSize = pageSize
      this.getEmployeeList()
    },
    async getEmployeeList() {
      const res = await getEmployeeListAPI(this.params)
      this.employeeList = res.data.rows
      this.total = res.data.total
    }
  },
  mounted() {
    this.getEmployeeList()
    this.getRoleList()
  }
}
</script>

<style lang="scss" scoped>
.box {
  // 浮动
  // flex 弹性容器布局（快速实现子元素横向布局）
  display: flex;
  // 默认是从左往右 从右往坐
  justify-content: flex-end;
  // float: right;
}

.employee-container {
  padding: 10px;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, .9);
  ;
  padding-bottom: 20px;

  .search-label {
    width: 100px;
  }

  .search-main {
    width: 220px;
    margin-right: 10px;
  }
}

.create-container {
  margin: 10px 0px;
}

.page-container {
  padding: 4px 0px;
  text-align: right;
}

.form-container {
  padding: 0px 80px;
}
</style>